package com.wan37.client.message.received;

import com.google.protobuf.Message;
import com.googlecode.protobuf.format.JsonFormat;
import com.wan37.client.service.cache.UserServiceCache;
import com.wan37.protobuf.constant.CommonEmail;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author : luoyong
 * @date : 2020-07-28 17:47
 **/
@Component(PtoNum.EMAILRESPONSE + "")
public class EmailResponse implements ReceiveMsg{
    private final Logger logger = LoggerFactory.getLogger(EmailResponse.class);
    @Autowired
    private UserServiceCache userServiceCache;
    @Override
    public void execute(Message msg) {
        GameEmail.EmailResponse emailResponse = (GameEmail.EmailResponse) msg;
        int code = emailResponse.getCode();
        switch (code){
            case CommonEmail.GET_ALL:
                if (emailResponse.getEmailList() != null){
                    logger.info("获取全部历史邮件！");
                    for (GameEmail.Email email : emailResponse.getEmailList()){
                        userServiceCache.getEmailMap().put(email.getEmailId(), email);
                        logger.info("邮件：" + JsonFormat.printToString(email));
                    }
                }
                break;
            case CommonEmail.SAVE:
                for (GameEmail.Email email : emailResponse.getEmailList()){
                    userServiceCache.getEmailMap().put(email.getEmailId(), email);
                    logger.info("收到一份邮件：" + JsonFormat.printToString(email));
                }
                break;
            case CommonEmail.CLAIM_ERROR:
                logger.info("邮件已经领取过了！！！");
                break;
            default:
                break;
        }
    }
}
